package com.longtop.sp.mapdao.dialect.oracle;

import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.jdbc.core.JdbcTemplate;

import com.longtop.sp.mapdao.dialect.mysql.MysqlPrimaryKey;
import com.longtop.sp.mapdao.dialect.postgresql.PostgresqlPrimaryKey;
import com.longtop.sp.mapdao.meta.IPrimaryKey;
import com.longtop.sp.mapdao.meta.PrimaryKeyFactory;
import com.longtop.sp.mapdao.util.ComparatorIgnoreCase;

public class OraclePrimaryKeyFactory implements PrimaryKeyFactory {
	private final Map syncPrimaryKeyPool = Collections
			.synchronizedMap(new TreeMap(new ComparatorIgnoreCase()));

	public IPrimaryKey createPrimarykey(JdbcTemplate jt, String tableName,String db_type)
			throws SQLException {
		if (syncPrimaryKeyPool.containsKey(tableName)) {
			return (IPrimaryKey) syncPrimaryKeyPool.get(tableName);
		} else if("mysql".equals(db_type)){
			IPrimaryKey pk = new MysqlPrimaryKey(jt, tableName);
			syncPrimaryKeyPool.put(tableName, pk);
			return pk;
		}else if("postgresql".equals(db_type)){
			IPrimaryKey pk = new PostgresqlPrimaryKey(jt, tableName);
			syncPrimaryKeyPool.put(tableName, pk);
			return pk;
		}else if("DB2".equals(db_type)){
			IPrimaryKey pk = new OraclePrimaryKey(jt, tableName);
			syncPrimaryKeyPool.put(tableName, pk);
			return pk;
		}else {
			IPrimaryKey pk = new OraclePrimaryKey(jt, tableName);
			syncPrimaryKeyPool.put(tableName, pk);
			return pk;
		}
	}

}